package com.kgcx.price.controller;

import com.kgcx.common.bean.ReturnData;
import com.kgcx.common.controller.BaseController;
import com.kgcx.price.domain.PriceCaculationPrice;
import com.kgcx.price.service.PriceCaculationPriceService;
import com.xiaoleilu.hutool.util.ObjectUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(description = "预估价计算相关接口", tags = "预估价计算相关接口")
@RestController
@RequestMapping("caculationprice")
public class PriceCaculationPriceController extends BaseController {

    @Autowired
    private PriceCaculationPriceService priceCaculationPriceService;

    @ApiOperation(value = "预估价计算接口", httpMethod = "POST", notes = "这是用来计算预估价的接口")
    @PostMapping("caculatePrice")
    public ReturnData<PriceCaculationPrice> caculatePrice(@RequestBody PriceCaculationPrice priceCaculationPrice) {
        return new ReturnData<>(priceCaculationPriceService.caculatePrice(priceCaculationPrice));
    }


    @ApiOperation(value = "获取预估价信息接口", httpMethod = "GET", notes = "这是用来获取预估价信息的接口")
    @GetMapping("getPriceCaculationPriceByOrderId")
    public ReturnData<PriceCaculationPrice> getPriceCaculationPriceByOrderId(@RequestParam(name = "orderId", required = true) String orderId) {
        PriceCaculationPrice priceCaculationPrice = priceCaculationPriceService.getPriceCaculationPriceByOrderId(orderId);
        if (null != priceCaculationPrice) {
            Long tokenKid = this.getTokenKid();
            if (null != tokenKid) {
                if (ObjectUtil.notEqual(priceCaculationPrice.getCustomerId(), tokenKid) && ObjectUtil.notEqual(priceCaculationPrice.getDriverId(), tokenKid)) {
                    return ReturnData.NO_PERMISSION;
                }
            }
        }
        return new ReturnData<>(priceCaculationPrice);
    }


    @GetMapping("test")
    @ApiImplicitParams({@ApiImplicitParam(name = "qos", value = "发布消息服务质量", paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "payload", value = "消息内容", paramType = "query", dataType = "String")})
    public void test(int qos, String payload) {
//        System.out.println(driverClient.getDriver(1L, "123"));

//        while(true)
//        {
//            Executors.newFixedThreadPool(4).execute(new Runnable() {
//                @Override
//                public void run() {
//                    try
//                    {
//
//
//
//                        Thread.sleep(2000);
//                    } catch (Exception e) {
//                        e.printStackTrace();
//                    }
//                }
//
//            });
//        }
        try {
            priceCaculationPriceService.test(qos, payload);
        } catch (MqttException e) {
            e.printStackTrace();
        }

    }

    @GetMapping("testCus")
    public void testCus() {
        priceCaculationPriceService.testCus();
    }
}
